﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using IBS.Model;

namespace IBS.MockDac
{
    public class IbsContextMock : IIbsContext
    {
        #region Fields

        private IObjectSet<Account> account;
        private IObjectSet<Address> address;
        private IObjectSet<Balance> balance;
        private IObjectSet<Client> client;
        private IObjectSet<Currency> currency;
        private IObjectSet<CurrencyExchangeRate> currencyExchangeRate;
        private IObjectSet<Request> request;
        private IObjectSet<Street> street;
        private IObjectSet<Client_User> client_users;
        private IObjectSet<CodeCard> code_cards;
        private IObjectSet<Card> cards;
        private IObjectSet<Payment> payments;
        private IObjectSet<Transfer> transfers;
        private IObjectSet<AutoPayment> autoPayments;

        #endregion

        public IbsContextMock()
        {
            CreateStreets();
            CreateAddresses();
            CreateClients();
            CreateRequests();
            CreateCodeCards();
            CreateCurrencies();
            CreateAccounts();
            CreatePayments();
            CreateCards();
            CreateAutoPayments();
        }

        private void CreateAutoPayments()
        {
            var autoPaymetn = new AutoPayment
                                  {
                                      Id = 1,
                                      Payment = Payments.Where(p=>p.Account.Currency.ISO == "USD").FirstOrDefault(),
                                      DayOfMonth = 1,
                                      PaymentDetail = "<xml>Stub<xml>",
                                      Amount = 1,
                                      Card = Cards.FirstOrDefault()
                                  };
            var autoPaymetn2 = new AutoPayment
            {
                Id = 2,
                Payment = Payments.Where(p => p.Account.Currency.ISO == "BYR").FirstOrDefault(),
                DayOfMonth = 1,
                PaymentDetail = "<xml>Stub<xml>",
                Amount = 1,
                Card = Cards.FirstOrDefault()
            };
            AutoPayments.AddObject(autoPaymetn);
            AutoPayments.AddObject(autoPaymetn2);
        }

        private void CreateCards()
        {
            var card = new Card()
                           {
                               Account = Accounts.Single(a => a.Id == 12),
                               Client = Clients.Single(c => c.Id == 1),
                               Type = (int)CardType.VisaClassic,
                               CreatedDate = DateTime.Now,
                               Number =  "12345"

                           };
            var card2 = new Card()
            {
                Account = Accounts.Single(a => a.Id == 10),
                Client = Clients.Single(c => c.Id == 1),
                Type = (int)CardType.VisaClassic,
                CreatedDate = DateTime.Now,
                Number = "12345567"

            };


            Cards.AddObject(card);
            Cards.AddObject(card2);

        }

        private void CreatePayments()
        {
            //Erip
            var p1 = new Payment()
                     {
                         Account = Accounts.Single(a => a.Id == 21),
                         Id = 1,
                         PaymentDetailTemplate = "<xml>COSMOS</xml>"
                     };

            var p2 = new Payment()
            {
                Account = Accounts.Single(a => a.Id == 21),
                Id = 2,
                PaymentDetailTemplate = "<xml>SOLO</xml>"
            };

            //Not erip
            var p3 = new Payment()
                     {
                         Account = Accounts.Single(a => a.Id == 12),
                         Id = 3
                     };

            //USA Payment
            var usaPayment = new Payment()
                                 {
                                     Id = 4,
                                     Account = Accounts.Where(a => a.Currency.ISO == "USD").FirstOrDefault()
                                 };

            //BYR Payment
            var blrPayment = new Payment()
            {
                Id = 5,
                Account = Accounts.Where(a => a.Currency.ISO == "BYR").FirstOrDefault()
            };
            Payments.AddObject(p1);
            Payments.AddObject(p2);
            Payments.AddObject(p3);
            Payments.AddObject(usaPayment);
            Payments.AddObject(blrPayment);
        }

        private void CreateCurrencies()
        {
            var usa = new Currency();
            usa.ISO = "USD";
            var rates = new EntityCollection<CurrencyExchangeRate>();
            rates.Add(new CurrencyExchangeRate()
                          {
                              AskRate = 8500,
                              BidRate = 8300,
                              Date = DateTime.Now
                          });
            usa.CurrencyExchangeRate = rates;

            var blr = new Currency();
            blr.ISO = "BYR";
            var blrRates = new EntityCollection<CurrencyExchangeRate>();
            blrRates.Add(new CurrencyExchangeRate()
            {
                AskRate = 1,
                BidRate = 1,
                Date = DateTime.Now
            });
            blr.CurrencyExchangeRate = blrRates;
            Currencies.AddObject(usa);
            Currencies.AddObject(blr);
        }

        private EntityCollection<Balance> GenerateBalance()
        {
            var result = new EntityCollection<Balance>();
            result.Add(new Balance()
                           {
                               Amount = 0,
                               Date = DateTime.Now
                           });
            return result;
        }

        private void CreateAccounts()
        {
            var eripAccount = new Account();
            eripAccount.Id = 21;
            eripAccount.Number = "123123";
            eripAccount.Currency = Currencies.Single(c => c.ISO == "BYR");
            eripAccount.Balance = GenerateBalance();


            var assetsAccount = new Account();
            assetsAccount.Id = 1;
            assetsAccount.Number = "12312314";
            assetsAccount.Currency = Currencies.Single(c => c.ISO == "BYR");
            assetsAccount.Balance = GenerateBalance();
            assetsAccount.Balance.FirstOrDefault().Amount = -200;


            var gAssetsAccount = new Account();
            gAssetsAccount.Id = 2;
            gAssetsAccount.Number = "31k32";
            gAssetsAccount.Currency = Currencies.Single(c => c.ISO == "USD");
            gAssetsAccount.Balance = GenerateBalance();
            gAssetsAccount.Balance.FirstOrDefault().Amount = -200;

            var usaSellingBase = new Account();
            usaSellingBase.Id = 3;
            usaSellingBase.Number = "aklf;adsf";
            usaSellingBase.Currency = Currencies.Single(c => c.ISO == "BYR");
            usaSellingBase.Balance = GenerateBalance();

            var usaSellingGAccount = new Account();
            usaSellingGAccount.Id = 4;
            usaSellingGAccount.Number = "ksafjlasd";
            usaSellingGAccount.Currency = Currencies.Single(c => c.ISO == "USD");
            usaSellingGAccount.Balance = GenerateBalance();

            var usaBuyingBase = new Account();
            usaBuyingBase.Id = 5;
            usaBuyingBase.Number = "kslacxz";
            usaBuyingBase.Currency = Currencies.Single(c => c.ISO == "BYR");
            usaBuyingBase.Balance = GenerateBalance();

            var usaBuyingGAccount = new Account();
            usaBuyingGAccount.Number = "USABuying";
            usaBuyingGAccount.Currency = Currencies.Single(c => c.ISO == "USD");
            usaBuyingGAccount.Id = 6;
            usaBuyingGAccount.Balance = GenerateBalance();

            var usaAccount = new Account();
            usaAccount.Id = 11;
            usaAccount.Number = "USA11";
            usaAccount.Currency = Currencies.Single(c => c.ISO == "USD");
            usaAccount.Balance = GenerateBalance();
            usaAccount.Balance.First().Amount = 100;

            var usaAccount2 = new Account();
            usaAccount2.Id = 10;
            usaAccount2.Number = "USA10";
            usaAccount2.Currency = Currencies.Single(c => c.ISO == "USD");
            usaAccount2.Balance = GenerateBalance();
            usaAccount2.Balance.First().Amount = 100;


            var blrAccount = new Account();
            blrAccount.Client = Clients.Single(c => c.Id == 1);
            blrAccount.Id = 12;
            blrAccount.Number = "BLR12";
            blrAccount.Currency = Currencies.Single(c => c.ISO == "BYR");
            blrAccount.Balance = GenerateBalance();
            blrAccount.Balance.First().Amount = 100;

            var blrAccount2 = new Account();
            blrAccount2.Client = Clients.Single(c => c.Id == 1);
            blrAccount2.Id = 13;
            blrAccount.Number = "BLR13";
            blrAccount2.Currency = Currencies.Single(c => c.ISO == "BYR");
            blrAccount2.Balance = GenerateBalance();
            blrAccount2.Balance.First().Amount = 100;

            Accounts.AddObject(assetsAccount);
            Accounts.AddObject(gAssetsAccount);
            Accounts.AddObject(usaSellingBase);
            Accounts.AddObject(usaSellingGAccount);
            Accounts.AddObject(usaBuyingBase);
            Accounts.AddObject(usaBuyingGAccount);
            Accounts.AddObject(usaAccount);
            Accounts.AddObject(usaAccount2);
            Accounts.AddObject(blrAccount);
            Accounts.AddObject(blrAccount2);
            Accounts.AddObject(eripAccount);
        }


        private void CreateCodeCards()
        {
            var codeCard1 = new CodeCard();
            codeCard1.C1 = "123";
            codeCard1.C2 = "234";
            codeCard1.C3 = "345";
            codeCard1.C4 = "2123";
            codeCard1.C5 = "412";
            codeCard1.CurrentPasswordNumber = 1;
            codeCard1.Id = 1;
            codeCard1.UserName = "";

            var codeCard2 = new CodeCard();
            codeCard2.C1 = "4123";
            codeCard2.C2 = "ds213";
            codeCard2.C3 = "23141";
            codeCard2.C4 = "qweqw";
            codeCard2.C5 = "23145";
            codeCard2.CurrentPasswordNumber = 1;
            codeCard2.Id = 2;
            codeCard2.UserName = "userWithCodeCard";

            CodeCards.AddObject(codeCard1);
            CodeCards.AddObject(codeCard2);
        }

        private void CreateRequests()
        {

            var request1 = new Request();
            request1.Status = (int)RequestStatus.New;

            request1.Email = "wheercool@gamil.com";
            request1.FirstName = "Генадий";
            request1.LastName = "Иванов";
            request1.Patronymic = "Сергеевич";
            request1.PassportNumber = 1233231;
            request1.PassportSeries = "MP";

            Requests.AddObject(request1);
        }

        private void CreateClients()
        {
            var client1 = new Client();
            client1.FirstName = "Вася";
            client1.PassportNumber = 1234567;
            client1.PassportSeries = "MP";
            client1.LastName = "Пупкин";
            client1.Patronymic = "Васильевич";
            client1.DateOfBirth = DateTime.Now.AddYears(-10);
            client1.Id = 1;
            client1.RegAddressId = 1;
            client1.HomeAddressId = 1;

            var client2 = new Client();
            client2.FirstName = "Александр";
            client2.LastName = "Пушкин";
            client2.Patronymic = "Сергеевич";
            client2.DateOfBirth = DateTime.Now.AddYears(-10);
            client2.Id = 2;
            client2.RegAddressId = 2;
            client2.HomeAddressId = 2;

            var client3 = new Client();
            client3.FirstName = "Владимир";
            client3.LastName = "Путин";
            client3.Patronymic = "Владимирович";
            client3.DateOfBirth = DateTime.Now.AddYears(-10);
            client3.Id = 3;
            client3.RegAddressId = 3;
            client3.HomeAddressId = 3;

            var bank = new Client();
            bank.FirstName = "bank";
            bank.DateOfBirth = DateTime.Now;
            bank.Id = 9;
            bank.RegAddressId = 1;
            bank.HomeAddressId = 1;

            Clients.AddObject(client1);
            Clients.AddObject(client2);
            Clients.AddObject(client3);
            Clients.AddObject(bank);
        }

        private void CreateAddresses()
        {
            var address1 = new Address();
            address1.Apartment = 1;
            address1.House = 1;
            address1.Housing = 1;
            address1.StreetId = 1;

            var address2 = new Address();
            address2.Id = 2;
            address2.Apartment = 1;
            address2.House = 1;
            address2.Housing = 1;
            address2.StreetId = 2;

            var address3 = new Address();
            address3.Apartment = 1;
            address3.House = 1;
            address3.Housing = 1;
            address3.StreetId = 3;

            Addresses.AddObject(address1);
            Addresses.AddObject(address2);
            Addresses.AddObject(address3);
        }

        private void CreateStreets()
        {
            //Create Street DataBase
            var street1 = new Street();
            street1.Id = 1;
            street1.Name = "Пушкина";
            street1.LoweredName = "пушкина";

            var street2 = new Street();
            street2.Id = 2;
            street2.Name = "Артема";
            street2.LoweredName = "артема";

            var street3 = new Street();
            street3.Id = 3;
            street3.Name = "Артема";
            street3.LoweredName = "артема";

            Streets.AddObject(street1);
            Streets.AddObject(street2);
            Streets.AddObject(street3);
        }

        #region IIbsContext Members

        public System.Data.Objects.IObjectSet<Account> Accounts
        {
            get { return account ?? (account = new MockObjectSet<Account>()); }
        }

        public System.Data.Objects.IObjectSet<Address> Addresses
        {
            get { return address ?? (address = new MockObjectSet<Address>()); }
        }

        public System.Data.Objects.IObjectSet<Balance> Balances
        {
            get { return balance ?? (balance = new MockObjectSet<Balance>()); }
        }

        public System.Data.Objects.IObjectSet<Client> Clients
        {
            get { return client ?? (client = new MockObjectSet<Client>()); }
        }

        public System.Data.Objects.IObjectSet<Currency> Currencies
        {
            get { return currency ?? (currency = new MockObjectSet<Currency>()); }
        }

        public System.Data.Objects.IObjectSet<CurrencyExchangeRate> CurrencyExchangeRates
        {
            get { return currencyExchangeRate ?? (currencyExchangeRate = new MockObjectSet<CurrencyExchangeRate>()); }
        }

        public System.Data.Objects.IObjectSet<Request> Requests
        {
            get { return request ?? (request = new MockObjectSet<Request>()); }
        }

        public System.Data.Objects.IObjectSet<Street> Streets
        {
            get { return street ?? (street = new MockObjectSet<Street>()); }
        }

        public IObjectSet<Client_User> ClientUsers
        {
            get { return client_users ?? (client_users = new MockObjectSet<Client_User>()); }
        }

        public IObjectSet<CodeCard> CodeCards
        {
            get { return code_cards ?? (code_cards = new MockObjectSet<CodeCard>()); }
        }


        public IObjectSet<Card> Cards
        {
            get { return cards ?? (cards = new MockObjectSet<Card>()); }
        }

        public IObjectSet<Payment> Payments
        {
            get { return payments ?? (payments = new MockObjectSet<Payment>()); }
        }

        public IObjectSet<AutoPayment> AutoPayments
        {
            get { return autoPayments ?? (autoPayments = new MockObjectSet<AutoPayment>()); }
        }

        public IObjectSet<Transfer> Transfers
        {
            get { return transfers ?? (transfers = new MockObjectSet<Transfer>()); }
        }


        public int SaveChanges()
        {

            return 0;
        }

        #endregion

        #region IDispasible Members

        public void Dispose()
        {

        }

        #endregion






        public void Refresh(RefreshMode Model, object entity)
        {
            throw new NotImplementedException();
        }
    }


}
